<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\builder\Builder;
use app\admin\model\copy\CopyConfig;
use app\admin\model\copy\Copydir;
use app\admin\model\getdatas\bind\Lianbiao;
use app\admin\model\Modular;
use app\admin\model\MyAuth;
use app\admin\model\MyChildren;
use app\admin\model\MySubject;
use app\admin\model\project\Install;
use app\admin\model\project\MyProject;
use app\BaseController;
use think\Controller;
use think\facade\Db;
use think\facade\View;
use think\Request;

class Index extends BaseController
{
    public static function back($msg='成功',$code=200){
        return \view('public/error',['code'=>$code,'action'=>'','msg'=>$msg,'backs'=>1]);
    }



    public function envs(){
        echo '<pre>';
        print_r(env());
    }

    /**加载首页数据
     * @return \think\response\Redirect|\think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        if(!$user=session('adminuser')){
            return redirect('/admin/index/signin');
        }
        $mokuai = Db::name(getTable('mokuais'))
            ->where('status',1)
            ->select()->toArray();
        foreach ($mokuai as $key=>$value){
            $value['column'] = Lianbiao::getMokuaiChild('column',['mokuai_id'=>$value['id']]);
            $value['lianbiao'] = Db::name(getTable('models'))
                ->where('type','联表')
                ->where('mokuaiid',$value['id'])
                ->select()->toArray();
            $mokuai[$key] = $value;
        }
        $welcome['fangwenliang'] = getSysTem(3);
        if(!$welcome['fangwenliang']){
            $welcome['fangwenliang'] = 0;
        }
        $welcome['fujian'] = Db::name(getTable('enclosure'))
            ->count();
        $welcome['yonghu'] = Db::name(getTable('users'))
            ->count();
        $systems = Db::name(getTable('systems'))
//            ->group('en_name')
            ->select();
        $newsystems = [];
        $sne = [];
        foreach ($systems as $value){
            if(!in_array($value['en_name'],$sne)){
                $newsystems[] = $value;
                $sne[] = $value['en_name'];
            }
        }



        View::assign('systems',$newsystems);
        $fangwen = Db::name(getTable('systems'))
            ->where('id',3)
            ->find();
        if(!$fangwen['value']){
            $fangwen['value'] = 0;
        }
        $fangwen['value'] = $fangwen['value']+1;
        Db::name(getTable('systems'))
            ->where('id',3)
            ->update(['value'=>$fangwen['value']]);
//        echo '<pre>';
//        print_r(getmyauth(['model_id'=>1]));
//        exit;
        return view('index/index',['mokuai'=>$mokuai,'user'=>$user,'welcome'=>$welcome]);
    }

    /**加载欢迎页面
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function welcome(){
        $welcome['fangwenliang'] = getSysTem(3);
        if(!$welcome['fangwenliang']){
            $welcome['fangwenliang'] = 0;
        }
        $welcome['fujian'] = Db::name(getTable('enclosure'))
            ->count();
        $welcome['yonghu'] = Db::name(getTable('users'))
            ->count();
        return view('index/welcome',['welcome'=>$welcome]);
    }

    /**加载登录页面
     * @param Request $request
     * @return \think\response\Redirect|\think\response\View
     */
    public function signin(Request $request){
        return view();
    }

    /**登录函数
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function signins(Request $request){
        $data =$request->param();
//        return json_to_respos('失败',203,session('captcha'));
        $yanzhen = yanzhen($data,[
           'user|用户名'=>'require',
            'pwd|密码'=>'require',
            'code|验证码'=>'require|captcha'
        ]);
        if(!$yanzhen['code']){
            return json_to_respos($yanzhen['msg'],205);
        }
        $shuju = Db::name(getTable('admin_user'))
            ->where('user',$data['user'])
            ->find();
        if(!$shuju){
            return json_to_respos('用户不存在',205);
        }
        if(pwd_decode($data['pwd'],$shuju['pwd'])){
            session('adminuser',$shuju);
            return json_to_respos('登录成功');
        }else{
            return json_to_respos('账号或密码错误',206);
        }
    }

    /**加载选择模块页面
     * @param Request $request
     * @return \think\response\View
     */
    public function project(Request $request){
        return view('index/project');
    }

    /**加载添加模块页面
     * @return \think\response\View
     */
    public function addproject(){
        return Builder::form()->setAction('index/set_project')
//            ->setProject('input','name','项目名称')
//            ->setProject('input','dir','项目模块')
            ->setProject('input','author','作者')
            ->setProject('input','sql','数据库文件')
            ->setProject('input','img','图片地址','/static/admin/img/error.jpg')
            ->setButton('submit','提交')
            ->setButton('back','返回')
            ->fetch();
    }

    /**添加模块
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function set_project(Request $request){
        $data = $request->param();
        $bool = Install::send($data);
        if($bool){
            $shuju = include app_path().'config/app.php';
            $shuju['install'] = true;
            CopyConfig::changeConfig($shuju,app_path().'config/app.php');
            return $this->success('添加成功',[],'','/admin/index/index');
        }else{
            return $this->error('添加失败');
        }

    }

    /**出错页面
     * @param Request $request
     * @return \think\response\View
     */
    public function errors(Request $request){
        $code = $request->param('code');
        $action = $request->param('action',null);
        return view('public/error',['code'=>$code,'action'=>$action]);
    }

    /**安装项目
     * @param Request $request
     * @return \think\response\Json
     */
    public function install(Request $request){
        $data = $request->param('project',null);
        ini_set('max_execution_time','600');
        if(!$data){
            return json_to_respos('请传入要安装的模块',205);
        }
        $bool = Install::send($data);
//        return json_to_respos('成功',200);
//        return json($bool);
        if($bool){
            return $this->success('安装成功');
        }else{
            return $this->error('安装失败');
        }

    }

    /**登出操作
     * @return \think\response\Redirect
     */
    public function out(){
        session('adminuser',null);
//        session('modular',null);
        return redirect('index/signin');
    }

    /**加载消息页面
     * @param Request $request
     * @return string
     */
    public function xiaoxi(Request $request){
        return View::fetch();
    }

    /**导出项目
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     */
    public function daochuxiangmu(Request $request){
        ini_set('max_execution_time','600');
        $data = $request->param('project');
        $shuju = MyProject::xcopy('./uploads/'.$data,'../application/'.$data.'/static/uploads');
//        return $shuju;
        MyProject::xcopy('./static/'.$data,'../application/'.$data.'/static/static');
        MyProject::xcopy(base_path().'/admin/controller/'.$data,'../application/'.$data.'/admin');
//        return $data;
        $sql = MySubject::mybei($data);
//        return $sql;
        $array = getProjectConfig($data);
        $array['sql'] = $sql;
        $array['install'] = 0;
        MyProject::create($array);
        return $this->success('导出成功');
    }
    public function fuzhi(Request $request){
        $table = $request->param('table');
        $number = $request->param('number',20);
        $id = $request->param('id',1);
        $table = Db::name(getTable('models'))
            ->where('id',$table)
            ->find();
        $data = Db::name($table['table'])
            ->where('id',$id)
            ->find();
        unset($data['id']);
        for ($a=0;$a<(int)$number;$a++){
            Db::name($table['table'])
                ->insert();
        }
        return $this->success('复制成功');
    }
}